package com.chuangyang.fixboxmaster.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.Part;

/**
 * Created by Administrator on 2015/6/11.
 */
public class AddPartActivity extends BaseActivity implements View.OnClickListener {

    private EditText mPartNameET;
    private EditText mPartPriceET;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_part);
        initView();
        showInPutMethod();
    }

    private void initView() {

        mPartNameET = (EditText) findViewById(R.id.part_name);
        mPartPriceET = (EditText) findViewById(R.id.part_price);
        TextView mAddPartTV = (TextView) findViewById(R.id.add_part);
        mAddPartTV.setOnClickListener(this);
    }

    private boolean validateData() {
        String name = mPartNameET.getText().toString();

        String price = mPartPriceET.getText().toString();
        if (name == null || name.equals("")) {
            Toast.makeText(this, "请准确输入配件名称!", Toast.LENGTH_LONG).show();
            return false;
        }
        if (price == null || price.equals("")) {
            Toast.makeText(this, "请输入价格!", Toast.LENGTH_LONG).show();
            return false;
        }
        return true;
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.add_part:

                if (validateData()) {
                    Part part = new Part();
                    part.title = mPartNameET.getText().toString();
                    part.suggestPrice = Integer.parseInt(mPartPriceET.getText().toString());
                    part.bSelect = true;
                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("part", part);
                    intent.putExtras(bundle);
                    setResult(RESULT_OK, intent);
                    finish();
                }
                break;
        }
    }
    private void showInPutMethod() {
        new Handler().postDelayed(new Runnable() {
            public void run() {
                InputMethodManager imm = (InputMethodManager) mPartNameET.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(mPartNameET, InputMethodManager.SHOW_FORCED);
            }
        }, 100);
    }

    @Override
    protected void onPause() {
        super.onPause();
        hideInputMethod();
    }

    private void hideInputMethod() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mPartNameET.getWindowToken(), 0);
    }
}
